package com.inspur.ourhis_afterend.Controller;

import com.inspur.ourhis_afterend.Entity.Dept;
import com.inspur.ourhis_afterend.Entity.Doctors;
import com.inspur.ourhis_afterend.Service.DoctorsService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/doctors")
public class DoctorsController {

    @Autowired
    DoctorsService doctorsService;

    @ApiOperation("查询全部医生")
    @ApiParam("传入分页")
    @PostMapping("/findAll")
    public Result findAll(@RequestBody PageAction pageAction) {
        System.out.println(pageAction);
        try {
            return Result.success(doctorsService.findAll(pageAction));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiParam("传入查询条件")
    @ApiOperation("医生查询")
    @PostMapping("/search")
    public Result search(@Param("departId") String departId,
                         @Param("realName") String realName,
                         @Param("docId") String docId) {
        try {
            System.out.println(departId);
            return Result.success(doctorsService.search(departId, realName, docId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiOperation("添加医生")
    @PostMapping("/add")
    @ApiParam(value = "传入一个医生对象")
    public Result add(@RequestBody Doctors doctors) {
        System.out.println(doctors);
        try {
            doctorsService.add(doctors);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiOperation("修改医生")
    @PostMapping("/edit")
    @ApiParam(value = "传入一个医生对象")
    public Result edit(@RequestBody Doctors doctors) {
        System.out.println(doctors);
        try {
            doctorsService.edit(doctors);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiOperation("删除医生")
    @PostMapping("/del")
    @ApiParam(value = "传入一个医生id")
    public Result del(@Param("docId") String docId) {
        try {
            int flag = doctorsService.del(docId);
            if (flag > 0) {
                return Result.success("删除成功");
            } else {
                return Result.success("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    @ApiOperation("查询单个医生")
    @PostMapping("/findOne")
    @ApiParam(value = "传入一个医生id")
    public Result findOne(@Param("docId") String docId) {
        try {
            return Result.success(doctorsService.findOne(docId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }
}
